package com.hjm.ui.shopcart.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjm.ui.R;
import com.hjm.ui.shopcart.ShopCartItem;
import com.hjm.ui.shopcart.entity.ShopCartVO;
import com.hjm.ui.shopcart.entity.Store;

import java.util.List;

public class ShopCartStoreAdapter extends RecyclerView.Adapter<ShopCartStoreAdapter.ViewHolder>{

    private static final String TAG = "ShopCartStoreAdapter";
    List<Store> storeList;
    Context context;
    private TextView storeNameTextView;
    private CheckBox selectAllCheckBox;
    private ImageView toStoreImageView;
    private ShopCartItem.Listener listener;

    public ShopCartStoreAdapter(List<Store> storeList, Context context) {
        this.storeList = storeList;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_shop_cart_item_store, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Store store = storeList.get(position);
        boolean isSelectAll = false;
        List<ShopCartVO> shopCartVOList = store.getShopCartVOList();
        for (int i=0;i<shopCartVOList.size();i++){
            if (!shopCartVOList.get(i).isGoodsSelect()){
                isSelectAll = false;
                break;
            }
            isSelectAll = true;
        }
        if (isSelectAll){
            holder.selectAllCheckBox.setChecked(true);
        }else{
            holder.selectAllCheckBox.setChecked(false);
        }

        holder.storeName.setText(store.getName());

        holder.goodsRecycleView.setLayoutManager(new GridLayoutManager(context, 1)); // 设置为一列的网格布局
        ShopCartStoreGoodsAdapter shopCartStoreGoodsAdapter = new ShopCartStoreGoodsAdapter(context, store.getShopCartVOList());
        shopCartStoreGoodsAdapter.setListener(listener);
        holder.goodsRecycleView.setAdapter(shopCartStoreGoodsAdapter);

        holder.selectAllCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                selectAll(store,isChecked);
            }
        });

        holder.shopCartItemStoreLineLayout.setOnClickListener((v) -> {
                listener.onStoreNameClick(store.getId());
        });
    }
    void selectAll(Store store,boolean isChecked){
        for (ShopCartVO shopCartVO : store.getShopCartVOList()) {
            shopCartVO.setGoodsSelect(isChecked);
        }
        listener.selectStoreAllChanged(isChecked,store.getId());
    }

    @Override
    public int getItemCount() {
        return storeList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView storeName;

        RecyclerView goodsRecycleView;

        CheckBox selectAllCheckBox;
        ImageView toStoreImageView;
        LinearLayout shopCartItemStoreLineLayout;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            storeName = itemView.findViewById(R.id.text_view_shop_cart_store_name);
            selectAllCheckBox = itemView.findViewById(R.id.checkbox_shop_cart_select_all);
            goodsRecycleView = itemView.findViewById(R.id.recycler_view_shop_cart_store_goods);
            toStoreImageView = itemView.findViewById(R.id.image_view_shop_cart_store_in_arrow);
            shopCartItemStoreLineLayout = itemView.findViewById(R.id.linear_layout_shop_cart_item_store);
        }
    }
    public void setListener(ShopCartItem.Listener listener) {
        this.listener = listener;
    }
}
